﻿using System.IO;
using UnityEngine;
using UnityEditor;
using Framework.Define;
using Framework.Util;

namespace FrameworkEditor.UITools.UIGenerate
{
    public class UIGenerateScript : UIGenerateBase
    {
        private string mUIName = string.Empty;
        private bool mIsSubPanel = false;
        private string mMainPanelName = string.Empty;

        private string mDirectoryPath = string.Empty;

        private string mPanelFileName = string.Empty;
        private string mPanelFilePath = string.Empty;

        private string mControllerFileName = string.Empty;
        private string mControllerFilePath = string.Empty;

        private string mViewFileName = string.Empty;
        private string mViewFilePath = string.Empty;

        public UIGenerateScript(string pUIName, bool pIsSubPanel, string pMainPanelName)
        {
            mUIName = pUIName;
            mIsSubPanel = pIsSubPanel;
            if (!pIsSubPanel)
                mMainPanelName = pUIName;
            else
                mMainPanelName = pMainPanelName;

            mDirectoryPath = string.Format("{0}/{1}/{2}/Scripts/", Application.dataPath, FrameworkDefine.UIRootDirectoryPath, mMainPanelName);

            mPanelFileName = string.Format("{0}.cs", pUIName);
            mPanelFilePath = string.Format("{0}/{1}", mDirectoryPath, mPanelFileName);

            mControllerFileName = string.Format("{0}Controller.cs", pUIName);
            mControllerFilePath = string.Format("{0}/{1}", mDirectoryPath, mControllerFileName);

            mViewFileName = string.Format("{0}View.cs", pUIName);
            mViewFilePath = string.Format("{0}/{1}", mDirectoryPath, mViewFileName);
        }


        public void GenerateScript()
        {
            if (CheckExistFile(mPanelFilePath, mPanelFileName)
                && CheckExistFile(mControllerFilePath, mControllerFileName)
                && CheckExistFile(mViewFilePath, mViewFileName))
            {
                CreateDirectory(mDirectoryPath);
                CreatePanelFile();
                CreateControllerFile();
                CreateViewFile();
            }
        }

        private void CreatePanelFile()
        {
            string content = @"
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Framework.UI;

namespace Game.UI
{{
    public class {0} : PanelBase<{0}Controller, {0}View>
    {{
        protected override void OnInit()
        {{
            Debug.LogError(""{0} Init"");
        }}
    }}
}}";

            content = string.Format(content, mUIName);
            CreateFile(mPanelFileName, mPanelFilePath, content);
        }

        private void CreateControllerFile()
        {
            string content = @"
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Framework.UI;

namespace Game.UI
{{
    public class {0}Controller : ControllerBase<{0}View>
    {{
        protected override void OnInit()
        {{
            Debug.LogError(""{0}Controller Init"");
        }}
    }}
}}";

            content = string.Format(content, mUIName);
            CreateFile(mControllerFileName, mControllerFilePath, content);
        }

        private void CreateViewFile()
        {
            string content = @"
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Framework.UI;

namespace Game.UI
{{
    public class {0}View : ViewBase<{0}Controller>
    {{
        protected override void OnInit()
        {{
            Debug.LogError(""{0}View Init"");
        }}
    }}
}}";

            content = string.Format(content, mUIName);
            CreateFile(mViewFileName, mViewFilePath, content);
        }

        private void CreateFile(string pFileName, string pFilePath, string pContentString)
        {
            byte[] byteArray = UtilCollection.DataUtil.StringToByteArray(pContentString);
            FileStream fileStream = File.Create(pFilePath);
            fileStream.Write(byteArray, 0, byteArray.Length);
            fileStream.Close();
            Debug.Log(string.Format("{0} create succeed. File path is :{1}", pFileName, pFilePath));
        }
    }
}